﻿using System;
using System.Collections;
using System.IO;
using System.Data;
using System.Threading;
using BLL;
using Utils;
namespace UI.ModSalary
{
    public partial class EmpItemImport : System.Web.UI.Page
    {
        SetsItemBiz setsItemBiz = new SetsItemBiz();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                rbUpdate.Enabled = false;
                DefDicBiz defDicBiz = new DefDicBiz();
                defDicBiz.InitDropDownList("Wage_Sets", ddlSetsID, "1");
                InitCheckBox("1");
            }

        }


        protected void btnUpBaseSL_Click(object sender, EventArgs e)
        {
            if (fUpBaseSL.HasFile)
            {
                string itType = "";
                if (rbAppend.Checked) itType = "Append";
                if (rbUpdate.Checked) itType = "Update";
               
   
                string setsID = ddlSetsID.SelectedValue;
                if (setsID == "0")
                {
                    JSUtility.Alert(this, "请选择正确的工资帐套");
                    return;
                }
                string strFileName = Server.HtmlEncode(fUpBaseSL.FileName);
                string strExtension = Path.GetExtension(strFileName);
                if (strExtension != ".xls" && strExtension != ".xlsx")
                {
                    JSUtility.Alert(this, "请选择正确的Excel文件");
                    return;
                }
                string strUploadFileName = "~/log/EmpItemImport" + strExtension;
                if (System.IO.File.Exists(strUploadFileName)) System.IO.File.Delete(strUploadFileName);
                string servePath = Server.MapPath(strUploadFileName);
               
                try
                {
                    fUpBaseSL.SaveAs(servePath); 
                    Hashtable ht = new Hashtable();
                    SetsItemBiz setsItemBiz = new SetsItemBiz();
                    for (int i = 0; i < this.cbList.Items.Count; i++)
                    {
                        if (this.cbList.Items[i].Selected == true)
                        {
                            ht.Add(setsItemBiz.GetWageItem(setsID, cbList.Items[i].Value), cbList.Items[i].Value);
                        }
                    }
                    if (ExcelUtility.ImportEmpItem(strExtension, servePath, ht, itType, "tt"))
                        JSUtility.Alert(this, "导入正确");
                    else
                        JSUtility.Alert(this, "导入失败1");
                }
                catch
                { JSUtility.Alert(this, "导入失败2"); }
               
            }
        }

        protected void ddlSetsID_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitCheckBox(ddlSetsID.SelectedValue);
        }

        private void InitCheckBox(string setsID)
        {
            if (setsID != "")
            {
                cbList.Items.Clear();
                DataTable dt = setsItemBiz.GetItemDict(setsID);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    cbList.Items.Add(dt.Rows[i]["ItemName"].ToString());

                }


            }
        }


    }
}